const dayjs = require('dayjs');
const utc = require('dayjs/plugin/utc')
dayjs.extend(utc)

function formatDate(time) {
  if (`${time}`.length < 13) time = time * 1000;
  let timestamp = new Date(Number(time));
  // 2020-4-28 23:58:37
  const format = dayjs.utc(timestamp);
  format.format();
  let origin = format.local().format('YYYY-MM-DD HH:mm:ss');
  let date = formatTime(origin);
  return [...date, time / 1000]; // [ '2020-04-28', '2020-04-28 23:58:37' ]
}

function formatTime(time) {
  let arr = [];
  arr = time.split(' ')[0].split('-');
  arr[1].length < 2 ? (arr[1] = `0${arr[1]}`) : null;
  arr[2].length < 2 ? (arr[2] = `0${arr[2]}`) : null;
  return [arr.join('-'), `${arr.join('-')} ${time.split(' ')[1]}`];
}

module.exports = {
  formatDate
};
